var mysql=require('mysql');
var express=require('express');
var app=express();
var fs=require('fs');
var bq=require('body-parser');

app.use(bq.json());
app.use(bq.urlencoded({extended:false}));




var config={
	host 	:'localhost',
	user 	:'root',
	password:'123456',
	database:'test'
}


var user=[];

function getUser(){
	var connection=mysql.createConnection(config);
	connection.connect();
	var sql='SELECT * FROM user';
	connection.query(sql,function(err,data){
		err ? console.log(err.message):{};
		console.log('------------')
		console.log(data);
		
		console.log('------------')
		user=(data);
	})
	connection.end()
	// getUser();
	
}getUser();





//获取所传回的参数
// app.get('/sendInfo',function(req,res){
	app.post('/sendInfo',function(req,res){
	console.log(req.body)
	// console.log(req.query)
	let u=req.body.params;
	let confirm=user.filter(o=>o.username==u.username&&o.password==u.password);
	let k={};
	if(confirm.length){
		k.body=JSON.parse(JSON.stringify(confirm[0]));
		k.body.password=undefined;
		
		// k.body=confirm[0];
		k.success=true;
	}else{
		k.success=false;
	}
	// res.json(req.query);
	res.json(k);
	res.end();
	// console.log(JSON.stringify(confirm[0]))
})

//注册
function register(obj,fn){
	var connection=mysql.createConnection(config);
	connection.connect();
	var sql=`INSERT INTO user(username,password,sex)
	values('${obj.username}','${obj.password}','${obj.sex}')`
	connection.query(sql,function(err,data){
		if(err){
			console.log(err)
			// throw err;抛出异常
			// fn({'msg':err})
			
			fn(err,data)
		}else{
			// fn(data);
			fn(null,data);
			
		}
	})
	connection.end();
	
}

//删除
function del(ids,fn){
	var connection=mysql.createConnection(config);
	connection.connect();
	var sql=`DELETE FROM user WHERE id IN (${ids})`
	connection.query(sql,function(err,data){
		if(err){
			console.log(err)
			fn(err,data)
		}else{
			fn(null,data);
			
		}
	})
	connection.end();
	
}


//写接口 post
app.post('/register',function(req,res){
	// let u=req.body.params;
	// u.sex='男';
	// u.sex='女';
	let u=req.body;
	
	register(u,function(err,data){
		getUser();
		let k ={};
		k.success=err ? false:true;
		k.body=data;
		
		// if(err){
		// 	k.success=false;
		// }else{
		// 	k.success=true;
		// 	k.body=data;
		// }
		res.json(k);
		res.end();
		// getUser();
	})
	// getUser();
		
})
// app.get('/delete',function(req,res){
app.get('/delete',function(req,res){
	// console.log(req.query);
	let ids=req.query.ids;
	console.log(ids);
	del(ids,function(err,data){
		getUser();
		let k ={};
		k.success=err ? false:true;
		k.body=data;
		res.json(k);
		res.end();
	})
	
})






// function register(username,password,sex){




//编写接口/getUser ,返回数据库数据（需要开启服务）

// app.get('/register',function(req,res){
// 	// console.log(req.query);
// 	var connection=mysql.createConnection(config);
	
// 	let user=req.query;
// 	console.log(user);
// 	connection.connect();
// 	var sql=`INSERT INTO user(username,password)
// 	values(${user.username},${user.password})`
	
// 	connection.query(sql,function(err,data){})
// 	connection.end();
// 	console.log('注册成功')
	
// })



app.get('/getUser',function(req,res){
	res.json(user);
	res.end()
}).listen(8000,function(){
	console.log('服务已经开启，冲 冲 冲！')
	
})